<?php
/**
 * @filename UEextAction.php
 * @author pangxl <pangxiaolong@mufengcm.com>
 * @datetime 2016-6-3  13:46:55
 * @Description
 * 对UEAction做了扩展
  */

namespace backend\controllers;

use Yii;
use yii\ueditor\UEditorAction;
use yii\web\UploadedFile;

class UEextAction extends UEditorAction
{
    protected function actionUpload()
    {
        if (isset($_GET['action'])) {
            switch ($_GET['action']) {
                case 'uploadimage':
                    $fileName = $this->config['imageFieldName'];
                    $uploader = UploadedFile::getInstanceByName($fileName);
                    $path = __DIR__ . '/../web';
                    $filePath = '/upload/image/'.date('Ymd');
                    if(!is_dir(sprintf('%s/%s',$path,$filePath))){
                        mkdir(sprintf('%s/%s',$path,$filePath),0777);
                    }
                    $fn = sprintf('%s/%s.%s',$path,$filePath,$uploader->extension);
                    if ($uploader->saveAs($fn)) {
                        echo json_encode([
                            'state' => 'SUCCESS',
                            'url' => sprintf('%s%s.%s',Yii::$app->request->hostInfo,$filePath,$uploader->extension)
                        ]);
                    }
                    break;
                case 'uploadfile':
                    $fileName = $this->config['imageFieldName'];
                    $uploader = UploadedFile::getInstanceByName($fileName);
                    $path = __DIR__ . '/../web';
                    $filePath = '/upload/file/'.date('Ymd');
                    if(!is_dir(sprintf('%s/%s',$path,$filePath))){
                        mkdir(sprintf('%s/%s',$path,$filePath),0777);
                    }
                    $fn = sprintf('%s/%s.%s',$path,$filePath,$uploader->extension);
                    if ($uploader->saveAs($fn)) {
                        echo json_encode([
                            'state' => 'SUCCESS',
                            'url' => sprintf('%s%s.%s',Yii::$app->request->hostInfo,$filePath,$uploader->extension)
                        ]);
                    }
                    break;
            }
        }
    }
}
